#include "include.h"

void tcp_config(void){
    /*  如果网络现在连着网*/
    if(system_status.net_status > 0){
        lastMsg_time = millis();
        client.stop();
        client.connect(tcp_config_parameter.ip_add, tcp_config_parameter.ip_port.toInt());
        while (!client.connected()) {
            delay(500);
            long now = millis();
            if (now - lastMsg_time > 10000) {
                /*  用户输出*/
                user_serial_print("error-parameter\r\n");
                return ;
            }
        }
    }
    // Serial.println("<<< tcp connect Ok!");
}

bool query_tcp_connect(void){
    return client.connected();
}

/*  用来查询 客户端是否接收到消息*/
void query_tcp_client_msg(void){
    String msg = "";
    if(client.connected()){
        /*  接受到数据*/
        while (client.available()) {
            char ch = static_cast<char>(client.read());
            msg.concat(ch);
        }
        /*  将数据放在缓冲区中*/
        system_status.client_CallBack_msg = msg;

        /* 如果缓冲区中 有数据，就发过去 */
        if(tcp_config_parameter.msg != ""){
            client.print(tcp_config_parameter.msg);
            tcp_config_parameter.msg = "";
        }
    }
    return;
}
